package com.css.fxfzaqbz.modules.data.service.impl;

import com.css.fxfzaqbz.base.upload.entity.AttachmentInfoEntity;
import com.css.fxfzaqbz.base.upload.service.AttachmentInfoService;
import com.css.fxfzaqbz.constants.YNEnum;
import com.css.fxfzaqbz.modules.assess2.entity.UrgencysettleData;
import com.css.fxfzaqbz.modules.data.entity.AidedAnalyse;
import com.css.fxfzaqbz.modules.data.repository.AidedAnalyseRepository;
import com.css.fxfzaqbz.modules.data.service.AidedAnalyseService;
import com.css.fxfzaqbz.modules.data.vo.AidedAnalyseVo;
import com.css.fxfzaqbz.util.*;
import com.css.fxfzaqbz.web.PlatformSessionContext;
import com.css.fxfzaqbz.zcpt.sys.entity.SUser;
import com.css.fxfzaqbz.zcpt.sys.service.SUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AidedAnalyseServiceImpl implements AidedAnalyseService {
    @Resource
    SUserService sUserService;
    @Resource
    AidedAnalyseRepository aidedAnalyseRepository;
    @Resource
    AttachmentInfoService attachmentInfoService;

    @Override
    public void saveAidedAnalyse(AidedAnalyse aidedAnalyse) {
        aidedAnalyse.setId(UUIDGenerator.getUUID());
        aidedAnalyse.setDelFlag(YNEnum.N.toString());
        aidedAnalyse.setCreateUser(PlatformSessionUtils.getUserId());
        aidedAnalyse.setCreateTime(PlatformDateUtils.getCurrentTimestamp());
        aidedAnalyse.setGeneratedTime(PlatformDateUtils.getCurrentTimestamp());
        String user = sUserService.getSUser(PlatformSessionContext.getUserID());
        SUser sUser = CreateGson.createGson().fromJson(user, SUser.class);
        aidedAnalyse.setSalesman(sUser.getUserName());
        aidedAnalyse.setStatus(YNEnum.N.toString());
        aidedAnalyse.setTaskName("辅助分析");
        aidedAnalyseRepository.saveAidedAnalyse(aidedAnalyse);
    }

    @Override
    public Map<String, Object> getAidedAnalyseById(String id) {
        AidedAnalyse aidedAnalyse = aidedAnalyseRepository.getAidedAnalyseById(id);
        List<String> strings = new ArrayList<>();
        if (!PlatformObjectUtils.isEmpty(aidedAnalyse.getDescription())) {
            String[] split = aidedAnalyse.getDescription().split("&&");
            for (String s : split) {
                strings.add(s);
            }
            aidedAnalyse.setDescriptions(strings);
        }
        String pictureId = aidedAnalyse.getPictureId();
        AttachmentInfoEntity attach = attachmentInfoService.getAttach(pictureId);
        Map<String, Object> map = new HashMap();
        map.put("aidedAnalyse", aidedAnalyse);
        map.put("attach", attach);
        return map;
    }

    @Override
    public void delAidedAnalyse(List<String> ids) {
        String userID = PlatformSessionContext.getUserID();
        Timestamp timestamp = PlatformDateUtils.getCurrentTimestamp();
        aidedAnalyseRepository.delAidedAnalyse(ids, timestamp, userID);
    }

    @Override
    public AidedAnalyse updateAidedAnalyse(AidedAnalyse aidedAnalyse) {
        aidedAnalyse.setUpdateTime(PlatformDateUtils.getCurrentTimestamp());
        aidedAnalyse.setUpdateUser(PlatformSessionUtils.getUserId());
        return aidedAnalyseRepository.updateAidedAnalyse(aidedAnalyse);
    }

    @Override
    public List<AidedAnalyse> getAidedAnalyseByIaId(String iaId) {
        return aidedAnalyseRepository.getAidedAnalyseByIaId(iaId);
    }

    @Override
    public List<AidedAnalyse> getByIaId(String iaId) {
        List<AidedAnalyse> aidedAnalyses = aidedAnalyseRepository.getByIaId(iaId);
        for (AidedAnalyse aidedAnalyse : aidedAnalyses) {
            List<String> strings = new ArrayList<>();
            if (!PlatformObjectUtils.isEmpty(aidedAnalyse.getDescription())) {
                String[] split = aidedAnalyse.getDescription().split("&&");
                for (String s : split) {
                    strings.add(s);
                }
                aidedAnalyse.setDescriptions(strings);
            }
        }
        return aidedAnalyses;
    }

    @Override
    public void saveDescription(AidedAnalyseVo aidedAnalyseVo) {
        List<String> descriptions = aidedAnalyseVo.getDescriptions();
        String description = StringUtils.join(descriptions, "&&");
        aidedAnalyseRepository.saveDescription(aidedAnalyseVo.getIds(), description);
    }

    @Override
    public boolean check(AidedAnalyse aidedAnalyse) {
        List<AidedAnalyse> aidedAnalyses = aidedAnalyseRepository.check(aidedAnalyse);
        if (aidedAnalyses == null || aidedAnalyses.size() == 0) {
            return false;
        }
        return true;
    }

    @Override
    public void submitResult(String id) {
        AidedAnalyse aidedAnalyse = aidedAnalyseRepository.getAidedAnalyseById(id);
        List<AidedAnalyse> aidedAnalyses = aidedAnalyseRepository.getByEntity(aidedAnalyse);
        String description = "";
        if (aidedAnalyses != null && aidedAnalyses.size() > 0) {
            description = aidedAnalyses.get(0).getDescription();
        }
        aidedAnalyseRepository.submitResult(id, description);
    }

    @Override
    public void removeResult(String id) {
        AidedAnalyse aidedAnalyseById = aidedAnalyseRepository.getAidedAnalyseById(id);
        aidedAnalyseById.setDescription("");
        aidedAnalyseById.setSubmiter("");
        aidedAnalyseById.setSubmitPhone("");
        aidedAnalyseRepository.removeResult(aidedAnalyseById);
    }

    @Override
    public void finalSubmission(List<String> ids) {
        String user = sUserService.getSUser(PlatformSessionContext.getUserID());
        SUser sUser = CreateGson.createGson().fromJson(user, SUser.class);
        String userName = sUser.getUserName();
        String mobilePhone = sUser.getMobilePhone();
        Timestamp currentTimestamp = PlatformDateUtils.getCurrentTimestamp();
        aidedAnalyseRepository.finalSubmission(ids, userName, mobilePhone,currentTimestamp);
    }

    @Override
    public void saveAidedAnalyses(List<AidedAnalyse> aidedAnalyses) {
        for (AidedAnalyse aidedAnalysis : aidedAnalyses) {
            this.saveAidedAnalyse(aidedAnalysis);
        }
    }

    @Override
    public boolean UniquenessCheck(String iaId) {
        List<UrgencysettleData> urgencysettleDataList = aidedAnalyseRepository.UniquenessCheck(iaId);
        if (urgencysettleDataList == null || urgencysettleDataList.size() == 0) {
            return false;
        }
        return true;
    }
}
